package com.unitechs.appbiz.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.unitechs.appbiz.constant.CommonConstants;
import com.unitechs.appbiz.constant.WsMngApiURL;
import com.unitechs.appbiz.domain.service.WsMngService;
import com.unitechs.appbiz.web.req.GetWsListReq;
import com.unitechs.appbiz.web.resp.CountUnHandleWsResp;
import com.unitechs.appbiz.web.resp.GetWsListResp;
import com.unitechs.framework.persistence.vo.PageVO;
import com.unitechs.framework.persistence.vo.PageVOHelper;
import com.unitechs.framework.rest.bean.RestResponse;
import com.unitechs.framework.rest.controller.BaseRestController;
import com.unitechs.framework.validator.BeanValidator;
import com.unitechs.framework.validator.BeanValidatorResult;
import com.unitechs.orch.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liwen
 * @since 2019/8/26
 */
@RestController
@Api(value = "WsMngController", tags = "工单管理接口")
public class WsMngController extends BaseRestController {
    @Autowired
    private WsMngService wsMngService;

    /**
     * 2.4.2.待处理工单数量统计(新增)
     *
     * @return RestResponse
     */
    @PostMapping(WsMngApiURL.COUNT_UNHANDLE_WS)
    @ApiOperation(notes = "待处理工单数量统计", value = "待处理工单数量统计")
    RestResponse<List<CountUnHandleWsResp>> countUnHandleWs() {
        logger.info("countUnHandleWs starts... {}");

        try {
            //参数校验

            //具体操作 与 返回
            return wsMngService.countUnHandleWs();
        } catch (ServiceException se) {
            return RestResponse.addError(CommonConstants.ERROR_CODE, se.getMessage());
        } catch (Exception e) {
            //e.printStackTrace();
            logger.error("countUnHandleWs：", e);
            return RestResponse.addError(CommonConstants.ERROR_CODE, e.getMessage());
        }
    }

    /**
     * 2.4.3.工单信息列表(新增)
     *
     * @param getWsListReq 请求对象
     * @return RestResponse
     */
    @PostMapping(WsMngApiURL.GET_WS_LIST)
    @ApiOperation(notes = "工单信息列表", value = "工单信息列表")
    RestResponse<PageVO<GetWsListResp>> getWsList(@RequestBody GetWsListReq getWsListReq) {
        logger.info("GetWsListResp starts... {}", getWsListReq);

        try {
            //参数校验
            BeanValidatorResult validatorResult = BeanValidator.validateEntity(getWsListReq);
            if (validatorResult.hasErrors()) {
                throw new ServiceException(validatorResult.getAllErrorsToString());
            }

            //具体操作 与 返回
            PageHelper.startPage(getWsListReq.getPage().getPageNum(), getWsListReq.getPage().getPageSize());
            Page<GetWsListResp> page = (Page<GetWsListResp>) wsMngService.getWsList(getWsListReq);
            return RestResponse.ok(PageVOHelper.convert2PageVO(page));
        } catch (ServiceException se) {
            return RestResponse.addError(CommonConstants.ERROR_CODE, se.getMessage());
        } catch (Exception e) {
            //e.printStackTrace();
            logger.error("getWsList：", e);
            return RestResponse.addError(CommonConstants.ERROR_CODE, e.getMessage());
        }
    }

}
